<?php

class login extends ViewsInterface {

    protected $page;
    protected $value = array();

    public function __construct($function = null, $parameter = null) {

        $this->page = new Template();
        $this->value = $parameter;
        if ($parameter["para"]) {
            echo $this->execute($function, $parameter['para']);
        } else {
            echo $this->execute($function);
        }
    }

    public function staticPage($parm) {
        if (intval($parm)) {
            $log = new Template(intval($parm));
        } else {
            $log = new Template();
            $log->assign($this->value);
            $log->draw_string($parm);
        }
        //$log->setText();
        //return $log->outputHTML();
    }

    public function logUser() {
        $respuesta = array();
        $db = new connection();
        if (!isset($_POST['usuario'])) {
            $_SESSION['usuario'] = isset($_SESSION['usuario']) ? $_SESSION['usuario'] : 7;
        } else {
            $user = $_POST['usuario'];
            $pass = $_POST['hidpass'];
            $reg = new AlaniaUsuarioModel();
            $reg->setIdentificacion($user);
            if (count(AlaniaUsuarioModel::findByExample($db->db, $reg))) {
                foreach (AlaniaUsuarioModel::findByExample($db->db, $reg) as $member) {
                    $pass1 = md5($member->getPassword() . $_SESSION['token']);
                    if (isset($pass) && $pass == $pass1) {
                        $_SESSION['usuario'] = $member->getIdUsuario();
                    } else {
                        return $this->send_error("", "Contraseña invalida", "login");
                    }
                }
            } else {
                return $this->send_error("", "usuario no existe", "login");
            }
        }
        $user = new LoggedUser($_SESSION['usuario']);
        if ($user->isVisitor()) {
            $log = new Template();
            $log->draw_string("login");
        } else {
            $member = $user->getUser();
            $log = new Template(); //"logout");
            $nombre = $member->getNombre() . " " . $member->getApellido();
            $log->assign("nombre", $nombre);
            $log->assign("sexo", $member->getSexo() == 'M' ? 'o' : 'a');
            $log->draw_string("logout");
            $this->value['session']['started'] = true;
            //$this->value['session']['user'] = $_SESSION['usuario'];
            $this->getContent();
        }
    }

    public function unLogUser() {
        $this->page = new Template();
        $respuesta = array();
        $respuesta['mensaje'] = array();
        //$this->page->assign("div","login");
        $_SESSION['usuario'] = 7;
        unset($this->value['session']['user']);
        $this->value['session']['started'] = false;
        //return $log->outputPartialHtml("login") . $this->returnToken() . $this->getContent(); //$ht->json2html(json_encode($div)) . " <div id='hiddendiv'><span>" . $_SESSION['token'] . "</span></div>";
        $this->page->draw_string("login");
        $this->getContent();
    }

    public static function logging() {
        $db = new connection();
        $reg = new AlaniaUsuarioModel();
        $user = new LoggedUser($_SESSION['usuario']);
        if (!$user->isVisitor()) {

            $member = $user->getUser();
            //if (count(AlaniaUsuarioModel::findByExample($db->db, $reg))) {
            //foreach (AlaniaUsuarioModel::findByExample($db->db, $reg) as $member) {
            $nombre = $member->getNombre() . " " . $member->getApellido();
            $log = new Template();
            $log->assign("nombre", $nombre);
            $log->assign("sexo", $member->getSexo() == 'M' ? 'o' : 'a');
            $log->draw_string("logout");
            //}
            //}
        } else {
            $log = new Template();
            $log->draw_string("login");
        }
    }

    public function returnToken() {
        $_SESSION['token'] = md5(microtime());
        $tok = new Template(); //"token");
        $tok->set("token", $_SESSION['token']);
        $tok->draw_string("token");
        $this->getContent();
        //$tok->setText();
        //return $tok->outputPartialHtml("hiddendiv") . $this->logUser() . $this->getContent();
    }

    public function show_page() {
        $this->page = new Template();
        $this->page->assign($this->value);
        $this->page->draw_string("main");
        //$this->logUser();
        //return $this->page->outputHTML();
    }

    private function getLeftData() {
//        $this->page->assign($this->value);
//        $this->page->draw_string("sideleft");
        $this->return_page("sideleft");
    }

    private function getRightData() {
//        $this->page->assign($this->value);
//        $this->page->draw_string("sideright");
        $this->return_page("sideright");
    }

    public function getContentData() {
//        $this->page->assign($this->value);
//        $this->page->draw_string("content");
        $this->return_page("content");
    }

    private function getContent() {
        $this->getLeftData();
        $this->getContentData();
        $this->getRightData();
    }

    private function getLoopData($template, $is_solicitud = false) {
        if (!$is_solicitud) {
            $info = new AlaniaInformacionModel();
            $db = new connection();
            $arr = $info->fromContenidosModel($db->db, $template);
            $this->page = new Template();
            $this->page->assign($template, $arr);
        }
    }

    private function return_page($template) {
        $this->page = new Template();
        $this->getLoopData($template);
        $this->page->assign($this->value);
        $this->page->draw_string($template);
    }

    public function editarContenido() {
        $this->page = new Template();
        $db = new connection();
        $op = array();
        foreach (AlaniaContenidosModel::selectAll($db->db) as $value) {
            array_push($op, array("op" => $value->getId(), "titulo" => $value->getNombre()));
        }
        //$op = substr($op, 0, -1) . "]";
        $this->page->assign($this->value);
        $this->page->assign("lcontenido", $op);
        $this->page->draw_string("editarcontenido");
    }

    public function crearContenido() {
        $this->page = new Template();
        $this->page->assign('preubaselect',0);
        if (isset($_POST)) {
            foreach ($_POST as $key => $value) {
                $this->page->assign($key, $value);
            }
        }
        //$this->getLoopData($template);
        $this->page->assign($this->value);
        
        $this->page->draw_string("crearcontenido");
    }

}

?>